﻿using System;
using System.Collections.Generic;
using CSharpExt.Threading;

namespace CSharpExt.Collections.Concurrent
{
    public class ConcurrentQueue<T> : Queue<T>
    {
        #region Operations

        public new void Enqueue(T item)
        {
            lock (this)
            {
                base.Enqueue(item);
                this.Signal();
            }
        }

        public new T Dequeue()
        {
            lock (this)
            {
                return base.Dequeue();
            }
        }

        public bool TryDequeue(out T value)
        {
            lock (this)
            {
                if (Count == 0)
                {
                    value = default(T);
                    return false;
                }
                else
                {
                    value = base.Dequeue();
                    return true;
                }
            }
        }

        public T DequeueOrWait()
        {
            lock (this)
            {
                while (Count == 0)
                {
                    this.Wait(1000);
                }
                return base.Dequeue();
            }
        }

        #endregion
    }
}
